<?php
class Poling_con extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->library('wysiwyg');
        }
        function index(){
            //menampilkan halaman default
            //rencananya akan menampilkan tabel kelas yang ada dengan status belum (artinya baru ditawarkan)
            $this->tampil_pertanyaan();
        }

        function tampil_pertanyaan(){
            // menampilkan daftar pertanyaan
            $query = $this->db->get('pertanyaan');
            $row=$query->result_array();
            $data2['data']=$row;
            $data['script']=$this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Tampil Pertanyaan";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pertanyaan_display',$data2);
            $this->load->view('footer_view');
        }

        function tambah_pertanyaan(){
            //nambah pertanyaan
            // menambah pertanyaan
            $nama = $this->input->get('nama');
            $tipe = $this->input->get('tipe');
            $jumlah = $this->input->get('jumlah');
            $data2['nama'] = $nama;
            $data2['tipe'] = $tipe;
            $data2['jumlah'] = $jumlah;
            $data2['pilihan'] = null;
            $data2['mode'] = null;
            $data['script']=$this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Tambah Pertanyaan";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pertanyaan_form',$data2);
                    
            $this->load->view('footer_view');
        }
        function simpan_tambah_pertanyaan(){
            //melakukan insert database pertanyaan dan daftar pilihan
            $nama = $this->input->post('nama');
            $tipe = $this->input->post('tipe');
            $jawaban = $this->input->post('jawaban');
            $this->db->insert('pertanyaan', array('ISI_PERTANYAAN' => $nama, 'TIPE_PERTANYAAN' => $tipe));
            if($tipe!='essay')
                {
                    $query = $this->db->query('SELECT MAX(ID_PERTANYAAN) AS id FROM pertanyaan');
                    $row = $query->result_array();
                    for ($i=0;$i<count($jawaban);$i++)
                    {
                        $this->db->insert('daftar_pilihan', array('ID_PERTANYAAN' => $row[0]['id'], 'ISI_PILIHAN' => $jawaban[$i]));
                    }
                }
            redirect(base_url() . 'admin/poling_con/');
        }
        function edit_pertanyaan($id){
            //melakukan edit pertanyaan
            $query = $this->db->get_where('pertanyaan',array ('ID_PERTANYAAN'=>$id));
            $row = $query->result_array();
            $data2['nama']=$row[0]['ISI_PERTANYAAN'];
            $data2['tipe'] = $row[0]['TIPE_PERTANYAAN'];
            $data['script'] = $this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Edit Pertanyaan";
            $query = $this->db->query('SELECT * FROM daftar_pilihan WHERE ID_PERTANYAAN = '.$id);
            $row = $query->result_array();
            $data2['pilihan'] = $row;
            $data2['mode'] = "edit";
            $data2['id'] = $id;
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pertanyaan_form',$data2);
            $this->load->view('footer_view');
        }
        function simpan_edit_pertanyaan($id)
        {
            //simpan edit pertanyaan
            $data['script'] = $this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Edit Pertanyaan";
            $this->load->view('admin/admin_header_view', $data);
            $jawaban = $this->input->post('jawaban');
            $nama = $this->input->post('nama');
            $tipe = $this->input->post('tipe');
            $query = $this->db->query('UPDATE pertanyaan SET ISI_PERTANYAAN="'. $nama .'", TIPE_PERTANYAAN="'. $tipe .'" WHERE ID_PERTANYAAN = '.$id);
            $this->db->delete('daftar_pilihan', array('ID_PERTANYAAN' => $id));
            if ($tipe!="essay")
                for($i=0;$i<count($jawaban);$i++)
            $this->db->insert('daftar_pilihan', array('ID_PERTANYAAN' => $id,'ISI_PILIHAN' => $jawaban[$i]));
            redirect(base_url().'admin/poling_con/tampil_pertanyaan');
        }
        function hapus_pertanyaan($id){
            //menghapus pertanyaan dengan id=$id
            $this->db->delete('pertanyaan', array('ID_PERTANYAAN' => $id));
            $this->db->delete('daftar_pilihan', array('ID_PERTANYAAN' => $id));
            redirect(base_url().'admin/poling_con/tampil_pertanyaan');
        }
        function list_tema(){
            $query_all_tema = $this->db->query('select * from tema_poling');
            $all_tema = $query_all_tema->result_array();
            $this->load->library('table');
            $this->table->set_template(array('table_open' => '<table align="center" width="40%" border="1" cellpadding="1" cellspacing="1">'));
            $this->table->set_heading('No','Nama Tema', 'Aksi');
            for($i=0;$i<count($all_tema);$i++){
                $this->table->add_row(
                    $i+1, 
                    $all_tema[$i]['NAMA_TEMA'],
                    anchor(base_url().'admin/poling_con/edit_tema/'.$all_tema[$i]['ID_TEMA'],'EDIT')
                    .' | '.
                    anchor(base_url().'admin/poling_con/delete_tema/'.$all_tema[$i]['ID_TEMA'],'DELETE')
                    .' | '.
                    anchor(base_url().'admin/poling_con/tampil_isi_tema/'.$all_tema[$i]['ID_TEMA'],'TAMPIL ISI')
                    .' | '.
                    anchor(base_url().'admin/poling_con/isikan_tema/'.$all_tema[$i]['ID_TEMA'],'ISI TEMA')
                );
            }
            $data['tabel'] = $this->table->generate();
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/list_tema',$data);
            $this->load->view('footer_view');
        }
        function buat_tema(){
            $data['title']="Buat tema";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/buat_tema');
            $this->load->view('footer_view');
        }
        function buat_tema_process(){
            $nama_tema = $this->input->post('judul_tema');
            $this->db->insert('tema_poling',array('NAMA_TEMA'=>$nama_tema));
            redirect(base_url().'admin/poling_con/list_tema');
        }
        function delete_tema($id){
            $this->db->where('ID_TEMA',$id);
            $this->db->delete('tema_poling');
            
            $this->db->where('ID_TEMA',$id);
            $this->db->delete('daftar_pertanyaan');
            redirect(base_url().'admin/poling_con/list_tema');
        }
        function edit_tema($id){
            $data = $this->db->get_where('tema_poling',array('ID_TEMA'=>$id))->row_array();
            $display['nama_tema'] = $data['NAMA_TEMA'];
            $display['id_tema'] = $data['ID_TEMA'];
            $dataheader['title']="Edit tema ".$display['nama_tema'];
            $this->load->view('admin/admin_header_view', $dataheader);
            $this->load->view('admin/edit_tema',$display);
            $this->load->view('footer_view');
        }
        function edit_tema_process(){
            $id = $this->input->post('id_tema');
            $nama = $this->input->post('judul_tema');
            $this->db->where('ID_TEMA',$id);
            $this->db->update('tema_poling',array('NAMA_TEMA'=>$nama));
            redirect(base_url().'admin/poling_con/list_tema');
        }
        function tampil_isi_tema($id){
            $data_tema = $this->db->get_where('tema_poling',array('ID_TEMA'=>$id))->row_array();
            $display['id_tema']=$id;
            $display['nama_tema']=$data_tema['NAMA_TEMA'];
            $dataheader['title']="Isi tema ".$display['nama_tema'];
            
            // query daftar pertanyaan
            $query = "SELECT * FROM daftar_pertanyaan NATURAL JOIN pertanyaan WHERE ID_TEMA=".$id;
            $display['isi_tema'] = $this->db->query($query)->result_array();
            
            
            $this->load->view('admin/admin_header_view', $dataheader);
            $this->load->view('admin/isi_tema',$display);
            $this->load->view('footer_view');
        }
        function delete_isi_tema($id_tema,$id_pertanyaan){
            $cond = array(
                'ID_TEMA'=>$id_tema,
                'ID_PERTANYAAN'=>$id_pertanyaan
            );
            $this->db->delete('daftar_pertanyaan', $cond);
            redirect(base_url().'admin/poling_con/tampil_isi_tema/'.$id_tema);
        }
        function isikan_tema($id){
            $data_tema = $this->db->get_where('tema_poling',array('ID_TEMA'=>$id))->row_array();
            $display['id_tema']=$id;
            $display['nama_tema']=$data_tema['NAMA_TEMA'];
            
            $strquery='select * from pertanyaan where ID_PERTANYAAN not in 
                (select ID_PERTANYAAN from daftar_pertanyaan where ID_TEMA='.$id.')';
            $display['list_pilihan'] = $this->db->query($strquery)->result_array();
            
            $this->load->view('admin/admin_header_view');
            $this->load->view('admin/isikan_tema',$display);
            $this->load->view('footer_view');       
        }
        function proses_isi($id){
            $jum_pilihan = $this->input->post('jum_total');
            $arr_pil = $this->input->post('pilihan');
            for($i=0;$i<$jum_pilihan;$i++){
                if(isset($arr_pil[$i])){
                    $data = array(
                        'ID_TEMA'=>$id,
                        'ID_PERTANYAAN'=>$arr_pil[$i]
                    );
                    $this->db->insert('daftar_pertanyaan', $data);
                }
            }
            redirect(base_url().'admin/poling_con/tampil_isi_tema/'.$id);
        }
    }

?>
